package com.yj.suncity.collect;

import java.util.List;

import org.json.JSONObject;

import com.yj.suncity.R;
import com.yj.suncity.activity.shouye.ProductDateilsActivity;
import com.yj.suncity.async.AsyncTaskUtil;
import com.yj.suncity.async.AsyncTaskUtil.ResultCallBack;
import com.yj.suncity.collect.bean.CollectGoodsBean;
import com.yj.suncity.collect.bean.CollectListBean;
import com.yj.suncity.constant.URLCon;
import com.yj.suncity.utis.DialogUtils;
import com.yj.suncity.utis.ToastUtil;
import com.yj.suncity.utis.DialogUtils.ReshActivity;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class CollectDuihuanListActivity extends Activity {
	private Context context;
	private String userId;
	private ListView mList_collect_view;
	private DisplayImageOptions options;
	private ImageLoader imageLoader = ImageLoader.getInstance();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_collect_list);
		context = this;
		userId = getIntent().getStringExtra("userid");
		findView();
		initData();
	}

	private void findView() {
		((TextView) findViewById(R.id.txt_title_name)).setText("收藏商家商品");
		TextView emptyView = (TextView) findViewById(R.id.txt_empty);
		mList_collect_view = (ListView) findViewById(R.id.list_collect_view);
		mList_collect_view.setEmptyView(emptyView);
	}

	private void initData() {
		String path = URLCon.收藏 + "?action=getgoodscollectionlist&userid="
				+ userId;
		Log.d("", path);
		AsyncTaskUtil asyncTaskUtil = new AsyncTaskUtil(context, null, null,
				false, null, true, "正在加载......", new ResultCallBack() {

					@Override
					public void JSONResult(JSONObject jsonObject) {
						CollectListBean listBean = new CollectListBean(
								jsonObject);
						mList_collect_view.setAdapter(new DigouAdapter(listBean
								.getGoodsData()));
					}
				});
		asyncTaskUtil.execute(path);
	}

	class DigouAdapter extends BaseAdapter {
		private List<CollectGoodsBean> goodsBeans;

		public DigouAdapter(List<CollectGoodsBean> goodsBeans) {
			super();
			this.goodsBeans = goodsBeans;
		}

		@Override
		public int getCount() {
			return goodsBeans.size();
		}

		@Override
		public Object getItem(int position) {
			return goodsBeans.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = LayoutInflater.from(context).inflate(
						R.layout.adapter_goods_item, null);
				holder.mImg_logo = (ImageView) convertView
						.findViewById(R.id.img_logo);
				holder.mTxt_name = (TextView) convertView
						.findViewById(R.id.txt_name);
				holder.mTxt_num = (TextView) convertView
						.findViewById(R.id.txt_digou_num);
				holder.mTxt_silver_num = (TextView) convertView
						.findViewById(R.id.txt_silver_num);
				holder.mLay_linear_goods = (LinearLayout) convertView
						.findViewById(R.id.lay_linear_goods);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			final CollectGoodsBean goodsBean = goodsBeans.get(position);
			holder.mTxt_name.setText(goodsBean.getGoods_title());
			holder.mTxt_num.setText(goodsBean.getGood_num());
			holder.mTxt_silver_num.setText(goodsBean.getReal_price());

			if (!goodsBean.getImg_url().equals("")) {
				if (goodsBean.getImg_url().contains(",")) {
					imageLoader.displayImage(
							URLCon.HOST
									+ (goodsBean.getImg_url().split(","))[0],
							holder.mImg_logo, options);
				} else {
					imageLoader.displayImage(
							URLCon.HOST + goodsBean.getImg_url(),
							holder.mImg_logo, options);
				}
			} else {
				holder.mImg_logo
						.setImageResource(R.drawable.android_layout_detail);
			}
			holder.mLay_linear_goods.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(context,
							ProductDateilsActivity.class);
					intent.putExtra("goodid", goodsBean.getId());
					intent.putExtra("flag", 2);
					startActivityForResult(intent,1);

				}
			});
			if (!userId.equals("")) {
				holder.mLay_linear_goods
						.setOnLongClickListener(new OnLongClickListener() {
							@Override
							public boolean onLongClick(View v) {
								DialogUtils dialogUtils = DialogUtils
										.getInstance();
								dialogUtils.backDialog(context, "确定删除吗？", true,
										new ReshActivity() {
											@Override
											public void reshActivity() {
												deleteCollect(goodsBean.getId());
											}
										});

								return false;
							}
						});
			}
			return convertView;
		}
	}

	class ViewHolder {
		ImageView mImg_logo;
		TextView mTxt_name;
		TextView mTxt_num;
		TextView mTxt_silver_num;
		LinearLayout mLay_linear_goods;
	}

	private void deleteCollect(String collectid) {
		String path = URLCon.收藏 + "?action=delcollection&userid=" + userId
				+ "&id=" + collectid;
		Log.d("", "删除：" + path);
		AsyncTaskUtil asyncTaskUtil = new AsyncTaskUtil(context, null, null,
				false, null, true, "正在删除......", new ResultCallBack() {

					@Override
					public void JSONResult(JSONObject jsonObject) {
						if (jsonObject.optString("success").equals("1")) {
							initData();
						} else {
							ToastUtil.ShowToast(context,
									"" + jsonObject.optString("msg"));
						}
					}
				});
		asyncTaskUtil.execute(path);
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnHeadBack:
			this.finish();
			break;

		default:
			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == 1 && resultCode == 1) {
            initData();
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
}
